package com.example.sofa_module.detail;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.example.common.databus.LiveDataBus;
import com.example.common.mvvm.BaseEntity;
import com.example.common.mvvm.viewmodel.BaseViewModel;
import com.example.common.providerservice.CheckLoginApi;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CommentViewModel extends BaseViewModel<CommentRepository> {
    public LiveDataBus<List<CommentEntity.DataBean.Comment>>commentList=new LiveDataBus<>();
    private CheckLoginApi checkLoginApi;
//    private long userId;
    @Override
    public void onResult(BaseEntity entity) {
        LogUtils.e("comment"+entity.message);
        if (entity instanceof CommentEntity){
            CommentEntity commentEntity= (CommentEntity) entity;
            List<CommentEntity.DataBean.Comment> data = commentEntity.data.data;
            commentList.postValue(data);
        }else if (entity instanceof AddComEntity){
            AddComEntity entity1= (AddComEntity) entity;
            if (entity1.message.equals("成功")){
                requestComment(0,1,10,0);
            }
        }else if (entity instanceof DelComEntity){
            DelComEntity delComEntity= (DelComEntity) entity;
            Boolean result = delComEntity.data.data.result;
            LogUtils.e("comment"+result);
            if (result){
                requestComment(0,1,10,0);
            }
        }
    }
    public void delComment(long commentId,long userId,long itemId){
        repository.requestDelComment(commentId,userId,itemId)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this);
    }
    @Override
    protected void initData() {
        checkLoginApi= (CheckLoginApi)ARouter.getInstance().build("/user/CheckLoginImpl").navigation();
//        userId=checkLoginApi.geUserId();
    }
    public void requestComment(int id,long itemId,int pageCount,long userId){
        repository.requestCommentList(id,itemId,pageCount,userId).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this);
    }
    public void requestAdd(String commentText,Integer height,String  image_url,Integer itemId,
                           Integer userId,String video_url,Integer width){
        if (checkLoginApi.checkLogin()){
            repository.requestAddComment(commentText,height,image_url,itemId,userId,video_url,width)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            ARouter.getInstance().build("/user/LoginActivity").navigation();
        }
    }
}
